"""Setup the chowa application"""
import logging

import pylons.test

from chowa.config.environment import load_environment
from chowa.model.meta import Session, Base
from chowa.lib.site.auth import add_admin, add_system, add_permissions, add_groups
from chowa.lib.site import add_pages

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup chowa here"""
    
    # Initialize translator
    from pylons.i18n.translation import _get_translator
    import pylons
    pylons.translator._push_object(_get_translator(pylons.config.get('lang')))

    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    Base.metadata.create_all(bind=Session.bind)
    
    # Add 'admin' user, permissions and groups
    admin_perms = [
        'site.administer.layout',
        'site.administer.navigation',
        'site.administer.security',
        'site.administer.organization',
        'site.administer.geo']
    add_permissions(admin_perms, Session)
    add_groups([('administrators', admin_perms)], Session)
    add_admin()
    add_system()
    add_pages('about', 'registration')
